package com.bjpowernode.p2p.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.p2p.model.loan.BidInfo;
import com.bjpowernode.p2p.model.loan.LoanInfo;
import com.bjpowernode.p2p.model.vo.Page;
import com.bjpowernode.p2p.service.BInfoService;
import com.bjpowernode.p2p.service.LoanService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:loanController
 * Package:com.bjpowernode.p2p.web
 * Description: 处理 查看各种 产品 的更多产品 的请求
 *
 * @date:2021/7/16 20:14
 * @author:动力节点
 */
@Controller
public class loanController {


    @Reference(interfaceClass = LoanService.class,version ="1.0-SNAPSHOT",check = false,timeout = 15000)
    private LoanService loanService;

    @Reference(interfaceClass = BInfoService.class,check = false,version = "1.0-SNAPSHOT",timeout = 15000)
    private BInfoService bInfoService;

    /**
     *
     * @param model
     * @param ptype 要展示的产品类型
     * @param currentPage 当前页，根据当前页去查询要展示的数据
     * @return 页面跳转
     */
    @RequestMapping("/loan/loan")
    public String queryMoreLoanInfo(Model model,
                                     String ptype,
                                     String currentPage){
        Page page = loanService.queryLoanInfoByPage(Integer.parseInt(ptype), currentPage);
        model.addAttribute("currentPage",page.getCurrentPage());//当前页
        model.addAttribute("pageData",page.getPageData());//当前页的数据
        model.addAttribute("totalRows",page.getTotalRows());//总记录数
        model.addAttribute("totalPageSize",page.getTotalPageSize());//总页数
        List pageData = page.getPageData();
        if (ObjectUtils.allNotNull(pageData)){
            LoanInfo loanInfo = (LoanInfo) pageData.get(0);
            Integer productType = loanInfo.getProductType();
            model.addAttribute("ptype",productType);
        }

        //TODO:loan.html的投资排行榜
        //投资排行榜：不登录也可以查看：展示前十名
        //查询binfo表
        List<BidInfo> bidInfoList =bInfoService.queryBidInfo();
        System.out.println(bidInfoList);
        model.addAttribute("investList",bidInfoList);
        return "loan";
    }

    /**
     * 根据 产品类型 和产品id 查询指定的产品并返回页面展示该产品
     * @param ptype
     * @param id
     * @return
     */
    @RequestMapping("/loan/loanInfo")
    public String queryInfoByType(Model model ,
                                  @RequestParam(value = "ptype",required = true) String ptype,
                                  @RequestParam(value = "id",required = true) String id){
        //查询展示产品的信息
        LoanInfo loanInfoByTypeId =loanService.queryLoanInfoByTypeandId(ptype,id);
        model.addAttribute("loanInfoByTypeId",loanInfoByTypeId);

        //查询 产品 的投资记录 ：b_bid_info
        List<BidInfo> incomeRecordList = bInfoService.queryUserByPruductId(id);
        model.addAttribute("incomeRecordList",incomeRecordList);
        return "loanInfo";
    }


}
